package com.homegoods.orderinfodetail.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.homegoods.orderinfodetail.R;
import com.homegoods.orderinfodetail.databinding.InfoActivityAddressManagerBinding;
import com.homegoods.orderinfodetail.model.AddressModel;
import com.homegoods.orderinfodetail.presenter.AddressManagerPresenter;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.resource.SpacesItemDecoration;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.base.MyBaseFragmentActivity;

/**
 * 我的收货地址
 *
 * @author QiQi
 */
@Route(path = ARouterPath.PATH_ADDRESS_MANAGER)
public class AddressManagerActivity extends MyBaseFragmentActivity<InfoActivityAddressManagerBinding, AddressManagerPresenter> {
    private RecyclerView recyclerView;

    private boolean isOrder;

    @Override
    public int getLayout() {
        return R.layout.info_activity_address_manager;
    }

    @Override
    protected void initEvent() {
        mPresenter.getAddresses();
        if (getIntent().getExtras() != null) {
            isOrder = getIntent().getBooleanExtra("isOrder", false);
        }
        if (isOrder) {
            mPresenter.getAdapter().setOnItemClickListener((MultiTypeAdapter.OnItemClickListener<AddressModel>) (v, iItem) -> {
                Intent intent = new Intent();
                intent.putExtra("AddressModel", JSON.toJSON(iItem.getData()).toString());
                setResult(RESULT_OK, intent);
                finish();
            });
        }
    }

    /**
     * app:layoutManager="android.support.v7.widget.LinearLayoutManager"
     * tools:itemCount="4"
     * tools:listitem="@layout/info_item_address_manager"
     *
     * @param savedInstanceState
     */
    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mBinding.setItem(mPresenter);
        recyclerView = mBinding.list;
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
//        recyclerView.setLayoutManager(MyUtils.getFlexBoxLayoutManager(this));
        recyclerView.addItemDecoration(new SpacesItemDecoration(2, 8));
        recyclerView.setAdapter(mPresenter.getAdapter());
    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        setTitle("我的收货地址");
    }

    @Override
    protected AddressManagerPresenter getPresenter() {
        return new AddressManagerPresenter(this);
    }

}
